/*
 * @Author: LIFM0623 1944099171@qq.com
 * @Date: 2024-02-24 16:14:55
 * @LastEditors: LIFM0623 1944099171@qq.com
 * @LastEditTime: 2024-02-28 15:05:23
 * @FilePath: \scene_back_nest\src\group\entities\group.entity.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  BeforeInsert,
  UpdateDateColumn,
} from 'typeorm';

@Entity()
export class Group {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column()
  group_id: string;

  @Column()
  group_name: string;

  @Column()
  announcement: string;

  @Column({ default: true })
  isApply: boolean;

  @Column({ default: true })
  joinWay: boolean;

  @Column({ default: null })
  avatar: string;

  @Column({ default: null })
  code: string;

  @Column({ default: null })
  expiration: Date;

  @Column({ default: null })
  catalog_uuid: string;

  @Column({
    name: 'create_time',
    type: 'timestamp without time zone',
    default: () => 'CURRENT_TIMESTAMP',
  })
  create_time: Date;

  @UpdateDateColumn({
    name: 'update_time',
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP',
  })
  update_time: Date;

  @BeforeInsert()
  setGroupId() {
    const timestamp = Date.now().toString(); // 获取当前时间戳并转换为字符串
    const randomNum = Math.floor(Math.random() * 10000); // 生成一个四位随机数
    const id = timestamp + randomNum; // 时间戳和随机数拼接
    this.group_id = id.slice(0, 16);
  }
}
